package occ.service
{
  import flash.events.EventDispatcher;
  import flash.events.IEventDispatcher;
  
  import occ.model.*;
  
  import mx.collections.ArrayCollection;
  import flash.data.*;
  import flash.events.*;
  import occ.utils.*;
   import mx.utils.*;
  

  public class Rentals extends EventDispatcher
  {
    public static const GET_RENTALS:String="GetRentalsEvent";
    public static const GET_RENTAL:String="onGetUniqueRental";
    public static const SAVE_RENTAL_RESULT:String="OnSaveRentalsResult";
    
    
    
    private var sqlData:Array;
		private var stmt:SQLStatement;
		private var sql:String;
		
		public var rentals:ArrayCollection;
		public var rental:RentalVO;
		
		public var lastSiteTypeId:int;
		
		
    public function Rentals(target:IEventDispatcher=null)
    {
      super(target);
    }
    
    public function getRentals():void
    {
      trace("getRentals");
      sql="select * from rentals";
      stmt = new SQLStatement();
			stmt.sqlConnection = DatabaseManager.getConnection();
			
			stmt.addEventListener( SQLErrorEvent.ERROR, onSQLError );
			stmt.addEventListener( SQLEvent.RESULT, onGetRentals );
			stmt.text = sql;
			stmt.execute();
    }
    
    private function onGetRentals(event:SQLEvent):void
    {
      trace("onGetRentals");
      
      var result:SQLResult = stmt.getResult();
      trace("look at result");
      trace(ObjectUtil.toString(result));
			rentals= new ArrayCollection();
      
      sqlData = new Array();
      try{
        sqlData = result.data;
        for(var i:int=0;i<sqlData.length;i++){
			  trace("in the loop " +  (sqlData[i] as Object).rental_id)
			  rental = new RentalVO();
			  rental.rentalId = (sqlData[i] as Object).rental_id;
			  rental.rentalName = (sqlData[i] as Object).rental_name;
			  rental.rentalDescr = (sqlData[i] as Object).rental_descr;
			  rental.baseRate=(sqlData[i] as Object).base_rate;
        rental.activeFlag=(sqlData[i] as Object).active_flag;
        rentals.addItem(rental);
			}
			 trace("siteTypes: " + rentals.length);
      }catch(e:Error){}
      
			
			dispatchEvent(new Event(GET_RENTALS));
			 
    }
    
    public function getRental(i:int):void
    {
       sql="select * from rentals where rental_id = " + i;
      stmt = new SQLStatement();
			stmt.sqlConnection = DatabaseManager.getConnection();
			
			stmt.addEventListener( SQLErrorEvent.ERROR, onSQLError );
			stmt.addEventListener( SQLEvent.RESULT, onGetRentalObject );
			stmt.text = sql;
			stmt.execute();
    }
    
    private function onGetRentalObject(event:SQLEvent):void
    {
      var result:SQLResult = stmt.getResult();
			rentals= new ArrayCollection();
			rentals = new ArrayCollection( result.data );
			for(var i:int=0;i<sqlData.length;i++){
			  rental = new RentalVO();
			  rental.rentalId = (sqlData[i] as Object).rental_id;
			  rental.rentalName = (sqlData[i] as Object).rental_name;
			  rental.rentalDescr = (sqlData[i] as Object).rental_descr;
        rental.activeFlag=(sqlData[i] as Object).active_flag;
        rental.baseRate=(sqlData[i] as Object).base_rate;
			  rentals.addItem(rental);
			}
			 //trace(" the cnt: " + (sqlData[0] as Object).name);
			dispatchEvent(new Event(GET_RENTAL));
			 
    }
    
    public function saveRental(_rental:RentalVO):void
    {
      trace("saveSiteType stvo: " + _rental.rentalId);
      if(_rental.rentalId == RentalVO.NULL_RENTAL_ID){
        trace("We should be inserting")
        sql =" insert into rentals "+
            " (rental_name, rental_descr, base_rate) "+
            " values "+
            " ('" + _rental.rentalName + "', '" + _rental.rentalDescr + "'," + _rental.baseRate+")";
        
      }else{
       sql=
       " update rentals set rental_name='"+ _rental.rentalName +"', "+ 
       "   rental_descr='"+ _rental.rentalDescr +"', "+
       "   base_rate="+ _rental.baseRate +
      " where rental_id= " + _rental.rentalId;
      }
      trace(sql);
      stmt = new SQLStatement();
			stmt.sqlConnection = DatabaseManager.getConnection();
			
			stmt.addEventListener( SQLErrorEvent.ERROR, onSQLError );
			stmt.addEventListener( SQLEvent.RESULT, onSaveRental );
			stmt.text = sql;
			stmt.execute();
    }
    
    private function onSaveRental(event:SQLEvent):void
    {
      dispatchEvent(new Event(SAVE_RENTAL_RESULT));
    }
    
    private function onSQLError( event:SQLErrorEvent ):void
		{
			 var errorString:String = "SQL Error: " + event.error.errorID + "nDetails: " + event.error.details + "nDetail Arguments: " +event.error.detailArguments;
 			  throw new Error(errorString);
		}
    
  }
}